<?php

namespace app\admin\model;

/**
 * 角色模型
 * Class AdminGroup
 * @package app\admin\model
 */
class AdminGroup extends Base
{

    //获取权限
    public function getMenusAttr($value)
    {
        return StrToArray($value);;
    }


    //全部角色列表
    public function groupList($keyword = ""){
        $getSubsetGroupIds = [];//子集
        if($this->admin["group_id"] != config("SuperAdminGroupId")) {
            $getSubsetGroupIds = $this->getSubsetGroupId($this->admin["group_id"]);
        }
        return $this
            ->where(function ($q)use ($keyword){
                if(!empty($keyword)){
                    $q->where("group_name","like","%".$keyword."%");
                }
            })
            ->where(config("TableDeleteField"),0)//获取没有删除的
            ->where(function ($q)use ($getSubsetGroupIds){
                if($this->admin["group_id"] != config("SuperAdminGroupId")) {
                    $q->where("group_id","in",$getSubsetGroupIds);
                }
            })
            ->select();
    }

    //获取子集角色
    public function getSubsetGroupId($group_id){
        $GroupSubsetIds = cache("GroupSubsetIds_".$group_id);//获取缓存
        $is = cache("GroupSubsetIds_".$group_id."_is");//获取缓存标识
        $result = [];
        if(empty($GroupSubsetIds) || $GroupSubsetIds === false || $is === false) {//缓存是否存在或者是否需要更新
            $list = collection($this->field("group_id")->where("parent_id", $group_id)->select())->toArray();
            foreach ($list as $value) {
                $value = $value["group_id"];//去掉数组键值对
                $result[] = $value;
                if ($this->where("parent_id", $value)->count()) {//还有子集追加
                    cache("GroupSubsetIds_".$value."_is",false);//设置子集缓存更新
                    $subsetList = $this->getSubsetGroupId($value);
                    $result = array_merge($result, $subsetList);
                }
            }
            //获取成功跟新缓存标识设置缓存
            cache("GroupSubsetIds_".$group_id,$result);//设置缓存
            cache("GroupSubsetIds_".$group_id."_is",true);//设置缓存标识
        }else{
            $result = $GroupSubsetIds;
        }
        return $result;
    }

    //获取全部上级ID
    public function getParentGroupId($group_id){
        $result = [];
        $parent_id = $this->where("group_id", $group_id)->value("parent_id");
        if ($parent_id != config("SuperAdminGroupId")) {//不是超级管理员就追加
            $result[] = $parent_id;
            $parentList = $this->getParentGroupId($parent_id);
            $result = array_merge($result, $parentList);
        }
        return $result;
    }

    //验证是否有权限访问
    public function checkAuthority($menu_id){
        $is = false;
        //超级管理员不需要验证
        if($this->admin["group_id"] != config("SuperAdminGroupId")) {
            $Menu = new Menu();
            $is_authority = $Menu->getFieldValue("menu_id",$menu_id,"is_authority");//获取是否验证
            if($is_authority == 1) {
                $menus = $this->where("group_id", $this->admin["group_id"])->value("menus");//获取角色权限
                if (!empty($menus)) {//验证是否有值
                    if (strpos($menus, "[" . $menu_id . "]") !== false) {//验证是否有权限访问
                        $is = true;
                    }
                }
            }else{
                $is = true;
            }
        }else{
            $is = true;
        }
        return $is;
    }

    //验证是否有权限修改
    public function checkSave($group_id,$pid){
        $is = false;
        //判断是否超级管理员
        if($this->admin["group_id"] != config("SuperAdminGroupId")) {
            $getSubsetGroupIds = $this->getSubsetGroupId($this->admin["group_id"]);//获取集合
            if(!empty($group_id)) {//修改验证
                if (in_array($group_id, $getSubsetGroupIds)) {
                    $is = true;
                }
            }
            if(!empty($pid)){//添加修改都验证
                //添加角色父级验证
                if (in_array($pid, $getSubsetGroupIds) || $this->admin["group_id"] == $pid) {
                    $is = true;
                }
            }
        }else{
            $is = true;
        }
        return $is;
    }

    //缓存更新
    public function cacheSave($group_id){
        if($group_id != config("SuperAdminGroupId")) {
            cache("GroupSubsetIds_".$group_id."_is",false);//设置缓存标识，需要更新
            $parentGroupIds = $this->getParentGroupId($group_id);//获取上级ID集合
            foreach ($parentGroupIds as $val){//循环全部上级ID
                cache("GroupSubsetIds_".$val."_is",false);//设置缓存标识，需要更新
            }
        }
    }

}